<?php
namespace app\api\model;

use app\common\helper\Encrypt;
use app\common\model\project_model\PaymentConfigModel as Payment;

class PaymentConfigModel extends Payment {

    /**
     * 根据applet_app_id返回key
     * @param $applet_app_id
     * @return bool|mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getKeyByAppletId($applet_app_id) {
        $model = new self();
        $one = $model->field('key')->where(['applet_app_id' => $applet_app_id])->find();
        if (empty($one)) {
            return false;
        }
        return Encrypt::decode($one['key'], config('encrypt_secret_key'));
    }

    /**
     * 根据过滤条件获取一条支付配置
     * @param $where
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($where) {
        $model = new self();
        $one = $model->where($where)->find();
        if (empty($one)) {
            return false;
        }
        $one = $one->getData();
        $one['key'] = Encrypt::decode($one['key'], config('encrypt_secret_key'));
        return $one;
    }

    public static function getKeyByWhere($where) {
        $model = new self();
        $one = $model->field('key')->where($where)->find();
        if (empty($one)) {
            return false;
        }
        return Encrypt::decode($one['key'], config('encrypt_secret_key'));
    }
}